import { EnumBjlResult } from "@/models/enums/EnumBjlResult";
import { ExcutePlan, RecmInfo, RoundInfo } from "../ExecutePlan";
import { FixedStrategy } from "../recmStrategy/FixedStrategy";
import { IMoneyStrategy } from "../IMoneyStrategy";
import { IPlan } from "../IExecutePlan";
import { WisdomGroupMoneyStrategyV2 } from "./WisdomMoneyStrategyV2";
import { NormalGroupMoneyStrategy } from "./NormalGroupMoneyStrategy";
import { WisdomGroupCombOption } from "./WisdomGroupCombMoneyStrategy";


export class WisdomMoneyStrategyV25 implements IMoneyStrategy {
    subPlan: IPlan[];
    debugCompoenent: string = "WisdomMoneyStrategyV2Debug";
    strategyName: string;
    startLevel: number = 10000;
    planArray: number[];
    reverse: boolean;
    resetMoney: number;
    prevSum: number;
    constructor(name?: string, option?: WisdomGroupCombOption) {
        this.strategyName = name ?? "WidomGroupCombMoneyStrategy" + (new Date()).getMilliseconds() + parseInt((Math.random() * 1000).toString());
        this.subPlan = [];

        if (option) {
            this.startLevel = option.startLevel;
            this.planArray = option.planArray;
            this.reverse = option.reverse;
            this.resetMoney = option.resetMoney;
        }
        else {
            this.startLevel = 10000;
            this.planArray = [1];
            this.reverse = false;
            this.resetMoney = 300;
        }
        this.prevSum = 0;
        this.resetStrategy();
    }
    showMoney(): [EnumBjlResult, number] {
        throw new Error("Method not implemented.");
    }
    resetRecm() {
        this.subPlan.forEach(item => {
            item.resetRecm();
            item.recmStrategy.forEach(rs => rs.resetRecm());
            item.moneyStrategy.forEach(ms => ms.resetRecm());
        });
    }
    resetStrategy(): void {
        this.prevSum = 0;

        let flPlan = new ExcutePlan();
        flPlan.planName = "复利";
        flPlan.recmStrategy = [new FixedStrategy({ reverse: false })]
        flPlan.moneyStrategy = [new NormalGroupMoneyStrategy("", {
            groupCount: 1,
            addStep: 100,
            devideStep: -100,
            reverse: false,
            maxLevel: 1000
        })];
        flPlan.toResult(new RoundInfo());

        let array = this.planArray;
        this.subPlan = [flPlan];
        for (let i = 0; i < array.length; i++) {
            let iv = array[i];
            let gi = new ExcutePlan();
            gi.planName = `正${iv}`;
            gi.baseMoney = 300;
            gi.recmStrategy = [new FixedStrategy({ reverse: false })];
            gi.relationPindex = [this.subPlan.length + 1];
            gi.moneyStrategy = [new WisdomGroupMoneyStrategyV2(`$wisdom-group-${i}`, {
                groupCount: (iv),
                addStep: 300,
                devideStep: -300,
                reverse: false, //true:赢了+100，输了-100
                maxLevel: 10000,
            })]
            gi.toResult(new RoundInfo());
            this.subPlan.push(gi);

            let gif = new ExcutePlan();
            gif.planName = `反${iv}`;
            gif.baseMoney = 300;
            gif.relationPindex = [this.subPlan.length - 1];
            gif.recmStrategy = [new FixedStrategy({ reverse: true })];
            gif.moneyStrategy = [new WisdomGroupMoneyStrategyV2(`$reverse-wisdom-group-${i}`, {
                groupCount: (iv),
                addStep: 300,
                devideStep: -300,
                reverse: true, //true:赢了+100，输了-100
                maxLevel: 10000,
            })]
            gif.toResult(new RoundInfo());
            this.subPlan.push(gif);

        }

    }
    toMoney(roundInfo: RoundInfo, recmInfo: RecmInfo, userRecm: RecmInfo, baseMoney: number): [EnumBjlResult, number] {
        let zm = 0;
        let xm = 0;
        let recmV = EnumBjlResult.U;
        let recmMoney = 0;
        let sumWin = 0;
        let groupWin = 0;
        let g0 = false;
        this.subPlan.forEach((pl, index) => {
            pl.setResult(roundInfo, pl.recmInfo, pl.multiple, pl.waterRate, pl.fuliRate);
            let userRecmV = pl.toResult(roundInfo);
            let newRoundInfo: RoundInfo = Object.assign({}, roundInfo, { userRecm: userRecmV });
            pl.userRecmInfo.recmV = userRecmV;
            let win = pl.recmInfo.summaryInfo.winMoney - pl.recmInfo.summaryInfo.failMoney;
            sumWin += win;

            if (index > 0) {
                groupWin += win;
            }
            if (index > 0 && index % 2 == 0) {
                let [rv, rm] = pl.toMoney(newRoundInfo, recmInfo, userRecm, pl.baseMoney);
                pl.recmInfo.preBetCount = pl.recmInfo.betCount;
                pl.recmInfo.recmV = rv;
                pl.recmInfo.recmMoney = rm;
                if (rv != EnumBjlResult.U) {
                    //pl.recmInfo.
                    pl.recmInfo.betCount++;
                }

                let giPlan = this.subPlan[index - 1] as ExcutePlan;
                let gifPlan = this.subPlan[index] as ExcutePlan;

                let giMoneyStrategy = giPlan.moneyStrategy[0] as WisdomGroupMoneyStrategyV2;
                let gifMoneyStrategy = gifPlan.moneyStrategy[0] as WisdomGroupMoneyStrategyV2;

                let giStartFlag = giMoneyStrategy && giMoneyStrategy.getLevel() > 0;
                let gifStartFlag = gifMoneyStrategy && gifMoneyStrategy.getLevel() > 0;
                //nrmMoneyStrategy.gameStart = true;
                // if (giStartFlag) {
                //     gifMoneyStrategy.stop();
                //     gifPlan.recmInfo.recmV = EnumBjlResult.U;
                // }
                // else if (gifStartFlag) {
                //     giMoneyStrategy.stop();
                //     giPlan.recmInfo.recmV = EnumBjlResult.U;
                // }

                // if (giMoneyStrategy.start && !gifMoneyStrategy.start) {
                //     let sum = giMoneyStrategy.getSum();
                //     if (sum == 0) {
                //         g0 = true;
                //     }
                // }
                // else if (gifMoneyStrategy.start && !giMoneyStrategy.start) {
                //     let sum = gifMoneyStrategy.getSum();
                //     if (sum == 0) {
                //         g0 = true;
                //     }
                // }
            }
            else {
                let [rv, rm] = pl.toMoney(newRoundInfo, recmInfo, userRecm, pl.baseMoney);
                pl.recmInfo.preBetCount = pl.recmInfo.betCount;
                pl.recmInfo.recmV = rv;
                pl.recmInfo.recmMoney = rm;
                if (rv != EnumBjlResult.U) {
                    //pl.recmInfo.
                    pl.recmInfo.betCount++;
                }
            }

        });
        let fl_plan = this.subPlan[0];
        let flwin = fl_plan.recmInfo.summaryInfo.winMoney - fl_plan.recmInfo.summaryInfo.failMoney;
        let reset = false;

        let flStrategy = fl_plan.moneyStrategy[0] as NormalGroupMoneyStrategy;
        if ((flStrategy.isTouchTop() && (sumWin <= 0 || sumWin > 10000))) {
            this.subPlan.forEach((pl, index) => {
                let plan = pl as ExcutePlan;
                if (plan) {
                    plan.resetPlan();
                    let userRecmV = plan.toResult(roundInfo);
                    let newRoundInfo: RoundInfo = Object.assign({}, roundInfo, { userRecm: userRecmV });
                    pl.userRecmInfo.recmV = userRecmV;

                    let [rv, rm] = plan.toMoney(newRoundInfo, recmInfo, userRecm, pl.baseMoney);
                    pl.recmInfo.preBetCount = pl.recmInfo.betCount;
                    pl.recmInfo.recmV = rv;
                    pl.recmInfo.recmMoney = rm;
                }
            });
        }

        if (this.reverse) {
            let flFlag = flwin < 0 ? (flwin + Math.abs(groupWin)) <= -300 : false;
            let groupFlag = groupWin < 0 ? (groupWin + Math.abs(flwin) <= -300) : false;
            reset = flFlag || groupFlag;
        }
        else {
            // let flFlag = flwin > 0 ? (flwin - Math.abs(groupWin)) >= 300 : false;
            // let groupFlag = groupWin > 0 ? (groupWin - Math.abs(flwin) >= 300) : false;
            // reset = flFlag || groupFlag;
        }

        if (reset || g0 || sumWin == 0 && sumWin != this.prevSum) {
            this.subPlan.forEach((pl, index) => {
                let plan = pl as ExcutePlan;
                if (plan) {
                    plan.resetPlan();
                    let userRecmV = plan.toResult(roundInfo);
                    let newRoundInfo: RoundInfo = Object.assign({}, roundInfo, { userRecm: userRecmV });
                    pl.userRecmInfo.recmV = userRecmV;

                    let [rv, rm] = plan.toMoney(newRoundInfo, recmInfo, userRecm, pl.baseMoney);
                    pl.recmInfo.preBetCount = pl.recmInfo.betCount;
                    pl.recmInfo.recmV = rv;
                    pl.recmInfo.recmMoney = rm;
                }
            });
        }
        this.prevSum = sumWin;


        this.subPlan.forEach((pl, index) => {
            pl.recmInfo.recmV = pl.recmInfo.recmV;
            pl.recmInfo.recmMoney = pl.recmInfo.recmMoney;

            if (pl.recmInfo.recmV == EnumBjlResult.Z) {
                zm += pl.recmInfo.recmMoney;

            }
            else if (pl.recmInfo.recmV == EnumBjlResult.X) {
                xm += pl.recmInfo.recmMoney;
            }
        });

        if (zm > xm) {
            recmV = EnumBjlResult.Z;
            recmMoney = zm - xm;
        }
        else if (xm > zm) {
            recmV = EnumBjlResult.X;
            recmMoney = xm - zm;
        }
        else {
            recmV = EnumBjlResult.U;
        }

        if (recmV != EnumBjlResult.U) {
            recmV = recmV == EnumBjlResult.Z ? EnumBjlResult.X : EnumBjlResult.Z;
        }

        return [recmV, recmMoney];
    }

    copyValue(valueObj: any): void {
        var obj = valueObj as WisdomMoneyStrategyV25;
        if (obj) {
            this.debugCompoenent = obj.debugCompoenent;
            this.prevSum = obj.prevSum;
            for (let i = 0; i < this.subPlan.length; i++) {
                this.subPlan[i].copyValue(obj.subPlan[i]);
            }
        }
    }
}